<?php
/**
 * Class that operate on table 'payment'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class PaymentMySqlDAO implements PaymentDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return PaymentMySql 
	 */
	public function load($payment){
		$sql = 'SELECT p.*,au.first_name,au.last_name from `payment` p left join admin_users au on p.admin_id=au.admin_id WHERE payment_id = "'.$payment->payment_id.'"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}
	public function all_payment($payment)
	{
		$sql = 'SELECT p.*,au.first_name,au.last_name from `payment` p left join admin_users au on p.admin_id=au.admin_id order by '.$payment->order_by.' '.$payment->sort_order.' limit '.$payment->results_per_page.' offset '.$payment->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	public function payment_count($payment)
	{
		$sql = 'SELECT count(*) as total FROM payment order by '.$payment->order_by.' '.$payment->sort_order.' limit '.$payment->results_per_page.' offset '.$payment->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM payment';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM payment ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param payment primary key
 	 */

	public function delete($payment){
		$sql = 'UPDATE payment SET status = "'.$payment->status.'" WHERE payment_id = "'.$payment->payment_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param PaymentMySql payment
 	 */
	public function insert($payment){
		$sql = 'INSERT INTO payment (admin_id, payment_gateway, payment_url, payment_otherinfo, created_date, updated_date) VALUES ("'.$payment->admin_id.'","'.$payment->payment_gateway.'",  "'.$payment->payment_url.'", "'.$payment->payment_otherinfo.'", "'.$payment->created_date.'", "'.$payment->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		//print_r($sql);exit;
		$id = $this->executeInsert($sqlQuery);	
		$payment->payment_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param PaymentMySql payment
 	 */
	public function update($payment){
		$sql = 'UPDATE payment SET admin_id = ?, payment_gateway = ?, payment_url = ?, payment_otherinfo = ?, status = ?, updated_date = ? WHERE payment_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($payment->admin_id);
		$sqlQuery->set($payment->payment_gateway);
		$sqlQuery->set($payment->payment_url);
		$sqlQuery->set($payment->payment_otherinfo);
		$sqlQuery->setNumber($payment->status);
		$sqlQuery->set($payment->updated_date);

		$sqlQuery->setNumber($payment->payment_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM payment';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByAdminId($value){
		$sql = 'SELECT * FROM payment WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPaymentGateway($value){
		$sql = 'SELECT * FROM payment WHERE payment_gateway = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPaymentUrl($value){
		$sql = 'SELECT * FROM payment WHERE payment_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPaymentOtherinfo($value){
		$sql = 'SELECT * FROM payment WHERE payment_otherinfo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM payment WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM payment WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM payment WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByAdminId($value){
		$sql = 'DELETE FROM payment WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPaymentGateway($value){
		$sql = 'DELETE FROM payment WHERE payment_gateway = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPaymentUrl($value){
		$sql = 'DELETE FROM payment WHERE payment_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPaymentOtherinfo($value){
		$sql = 'DELETE FROM payment WHERE payment_otherinfo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM payment WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM payment WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM payment WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return PaymentMySql 
	 */
	protected function readRow($row){
		$payment = new Payment();
		
		$payment->payment_id = $row['payment_id'];
		$payment->admin_id = $row['admin_id'];
		$payment->first_name = $row['first_name'];
		$payment->last_name = $row['last_name'];
		$payment->payment_gateway = $row['payment_gateway'];
		$payment->payment_url = $row['payment_url'];
		$payment->payment_otherinfo = $row['payment_otherinfo'];
		$payment->status = $row['status'];
		$payment->created_date = $row['created_date'];
		$payment->updated_date = $row['updated_date'];

		return $payment;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return PaymentMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>